<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="XV6：增加系统调用, python,machine learning,deep learning,html,css,c,c++,cpp,cmake,ros,linux,ubuntu">
    <meta name="description" content="本文介绍了如何为XV6增加系统调用">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>XV6：增加系统调用 | JackWang&#39;s Blog</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 5.4.2">
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/css/prism-line-numbers.css" type="text/css"></head>



   <style>
    body{
       background-image: url(https://cdn.jsdelivr.net/gh/Tokisaki-Galaxy/res/site/medias/background.jpg);
       background-repeat:no-repeat;
       background-size: 100% 100%;
       background-attachment:fixed;
    }
</style>



<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">JackWang&#39;s Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-book-reader" style="zoom: 0.6;"></i>
      
      <span>博客</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/tags">
          
          <i class="fas fa-tags" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按标签归类文章</span>
        </a>
      </li>
      
      <li>
        <a href="/categories">
          
          <i class="fas fa-bookmark" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按目录归类文章</span>
        </a>
      </li>
      
      <li>
        <a href="/archives">
          
          <i class="fas fa-archive" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按日期分类文章</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>



<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">JackWang&#39;s Blog</div>
        <div class="logo-desc">
            
            JackWang的个人博客
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-book-reader"></i>
			
			博客
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>

                  <a href="/tags " style="margin-left:75px">
				  
				   <i class="fa fas fa-tags" style="position: absolute;left:50px" ></i>
			      
                              <span>按标签归类文章</    span>

                  </a>
                </li>
              
                <li>

                  <a href="/categories " style="margin-left:75px">
				  
				   <i class="fa fas fa-bookmark" style="position: absolute;left:50px" ></i>
			      
                              <span>按目录归类文章</    span>

                  </a>
                </li>
              
                <li>

                  <a href="/archives " style="margin-left:75px">
				  
				   <i class="fa fas fa-archive" style="position: absolute;left:50px" ></i>
			      
                              <span>按日期分类文章</    span>

                  </a>
                </li>
              
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    
<script src="/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('抱歉，这篇文章并不想让所有人都看到，请输入授权密码观看')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221022160403946.png')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">XV6：增加系统调用</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/Operating-System/">
                                <span class="chip bg-color">Operating System</span>
                            </a>
                        
                            <a href="/tags/xv6/">
                                <span class="chip bg-color">xv6</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/xv6/" class="post-category">
                                xv6
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2022-10-22
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2023-06-01
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    5.1k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    23 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221022160403946.png" alt="为XV6增加系统调用"></p>
<h1 id="XV6：增加系统调用"><a href="#XV6：增加系统调用" class="headerlink" title="XV6：增加系统调用"></a>XV6：增加系统调用</h1><p>本文介绍了<code>XV6</code>操作系统（Unix Version 6）中系统调用的工作流程，此外也介绍了如何为<code>XV6</code>操作系统添加函数调用</p>
<h2 id="1-目标"><a href="#1-目标" class="headerlink" title="1. 目标"></a>1. 目标</h2><p>稍后我们会讲解<code>XV6</code>中的系统调用的工作原理，包括如何从用户态跳转到内核态，系统调用功能号等等。</p>
<p>但是光听不练假把式，所以为了更好的能够去理解<code>XV6</code>中系统调用的工作原理，我们还要实践一下，也就是说为<code>XV6</code>增加新的系统调用。</p>
<p>本文中我们需要增加两个系统调用，分别是<code>trace(const char *pathname)</code>和<code>getcount()</code></p>
<p>这两个系统调用的功能是，<code>trace</code>接受一个文件路径，此后系统开始追踪该文件，被追踪的文件每被打开一次，那么计数器就会加一，而<code>getcount</code>将会返回该文件被打开的次数。</p>
<p>两个系统调用的函数原型如下：</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">trace</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>pathname<span class="token punctuation">)</span>
<span class="token keyword">int</span> <span class="token function">getcount</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>接下来我们将首先介绍<code>XV6</code>中系统调用的原理，而后再增加这两个系统调用</p>
<h2 id="2-系统调用全流程"><a href="#2-系统调用全流程" class="headerlink" title="2. 系统调用全流程"></a>2. 系统调用全流程</h2><p>前面我们介绍了<code>XV6</code>项目的<code>Makefile</code>，我们从<code>Makefile</code>的<code>kernel</code>和<code>fs.img</code>这两个目标中就能够看出来，操作系统本身就是一个程序，而用户写的程序和操作系统这个大程序在地位上是相等的，大家都有<code>main</code>函数。</p>
<p>例如，内核的<code>main</code>函数如下</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// Bootstrap processor starts running C code here.</span>
<span class="token comment" spellcheck="true">// Allocate a real stack and switch to it, first</span>
<span class="token comment" spellcheck="true">// doing some setup required for memory allocator to work.</span>

<span class="token keyword">int</span>
<span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
  <span class="token function">kinit1</span><span class="token punctuation">(</span>end<span class="token punctuation">,</span> <span class="token function">P2V</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token operator">*</span><span class="token number">1024</span><span class="token operator">*</span><span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// phys page allocator</span>
  <span class="token function">kvmalloc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment" spellcheck="true">// kernel page table</span>
  <span class="token function">mpinit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment" spellcheck="true">// detect other processors</span>
  <span class="token function">lapicinit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment" spellcheck="true">// interrupt controller</span>
  <span class="token function">seginit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment" spellcheck="true">// segment descriptors</span>
  <span class="token function">picinit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment" spellcheck="true">// disable pic</span>
  <span class="token function">ioapicinit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment" spellcheck="true">// another interrupt controller</span>
  <span class="token function">consoleinit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment" spellcheck="true">// console hardware</span>
  <span class="token function">uartinit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment" spellcheck="true">// serial port</span>
  <span class="token function">pinit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment" spellcheck="true">// process table</span>
  <span class="token function">tvinit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment" spellcheck="true">// trap vectors      // == 中断向量表</span>
  <span class="token function">binit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment" spellcheck="true">// buffer cache</span>
  <span class="token function">fileinit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment" spellcheck="true">// file table</span>
  <span class="token function">ideinit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment" spellcheck="true">// disk </span>
  <span class="token function">startothers</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment" spellcheck="true">// start other processors</span>
  <span class="token function">kinit2</span><span class="token punctuation">(</span><span class="token function">P2V</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token operator">*</span><span class="token number">1024</span><span class="token operator">*</span><span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">P2V</span><span class="token punctuation">(</span>PHYSTOP<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// must come after startothers()</span>
  <span class="token function">userinit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment" spellcheck="true">// first user process        // == 开启第一个用户进程</span>
  <span class="token function">mpmain</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment" spellcheck="true">// finish this processor's setup</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>而用户编写的<code>ls</code>这个程序中的<code>main</code>函数如下</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token keyword">int</span>
<span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">int</span> argc<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span>argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
  <span class="token keyword">int</span> i<span class="token punctuation">;</span>

  <span class="token keyword">if</span><span class="token punctuation">(</span>argc <span class="token operator">&lt;</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">ls</span><span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">exit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>argc<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
    <span class="token function">ls</span><span class="token punctuation">(</span>argv<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">exit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>因此，操作系统和用户程序都是相互独立的、完整的程序。</p>
<p><strong>那么就存在一个问题，系统调用究竟是如何工作的呢？</strong></p>
<h3 id="A-不同视角下的系统调用"><a href="#A-不同视角下的系统调用" class="headerlink" title="A. 不同视角下的系统调用"></a>A. 不同视角下的系统调用</h3><p>在讲解系统调用的工作原理之前，我们先分别从<strong>用户程序（程序员）</strong>和<strong>操作系统（内核开发者）</strong>的角度分别来看看系统调用的样子</p>
<h4 id="User-Program’s-Developer’s-Perspective"><a href="#User-Program’s-Developer’s-Perspective" class="headerlink" title="User Program’s (Developer’s) Perspective"></a>User Program’s (Developer’s) Perspective</h4><p>我们首先从用户程序的视角来看看系统调用的样子。</p>
<p><code>test_trace.c</code>是我在已经写好了需要新增加的两个系统调用之后为了测试写的一个用户程序。</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// User program test_trace.c</span>

<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">"types.h"</span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">"stat.h"</span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">"user.h"</span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">"fcntl.h"</span></span>


<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">int</span> argc<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span>argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
  <span class="token keyword">int</span> <span class="token constant">stdout</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>

  <span class="token function">printf</span><span class="token punctuation">(</span><span class="token constant">stdout</span><span class="token punctuation">,</span> <span class="token string">"Start my syscall testing...\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token function">trace</span><span class="token punctuation">(</span><span class="token string">"ls"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">trace</span><span class="token punctuation">(</span><span class="token string">"pwd"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">trace</span><span class="token punctuation">(</span><span class="token string">"test"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">trace</span><span class="token punctuation">(</span><span class="token string">"666"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


  <span class="token function">printf</span><span class="token punctuation">(</span><span class="token constant">stdout</span><span class="token punctuation">,</span> <span class="token string">"ls count = %d\n"</span><span class="token punctuation">,</span> <span class="token function">getcount</span><span class="token punctuation">(</span><span class="token string">"ls"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">"ls"</span><span class="token punctuation">,</span> O_RDONLY<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">printf</span><span class="token punctuation">(</span><span class="token constant">stdout</span><span class="token punctuation">,</span> <span class="token string">"ls count = %d\n"</span><span class="token punctuation">,</span> <span class="token function">getcount</span><span class="token punctuation">(</span><span class="token string">"ls"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">"ls"</span><span class="token punctuation">,</span> O_RDONLY<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">printf</span><span class="token punctuation">(</span><span class="token constant">stdout</span><span class="token punctuation">,</span> <span class="token string">"ls count = %d\n"</span><span class="token punctuation">,</span> <span class="token function">getcount</span><span class="token punctuation">(</span><span class="token string">"ls"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">"ls"</span><span class="token punctuation">,</span> O_RDONLY<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">printf</span><span class="token punctuation">(</span><span class="token constant">stdout</span><span class="token punctuation">,</span> <span class="token string">"ls count = %d\n"</span><span class="token punctuation">,</span> <span class="token function">getcount</span><span class="token punctuation">(</span><span class="token string">"ls"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


  <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">"pwd"</span><span class="token punctuation">,</span> O_RDONLY<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">printf</span><span class="token punctuation">(</span><span class="token constant">stdout</span><span class="token punctuation">,</span> <span class="token string">"pwd count = %d\n"</span><span class="token punctuation">,</span> <span class="token function">getcount</span><span class="token punctuation">(</span><span class="token string">"pwd"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">"test"</span><span class="token punctuation">,</span> O_RDONLY<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">printf</span><span class="token punctuation">(</span><span class="token constant">stdout</span><span class="token punctuation">,</span> <span class="token string">"test count = %d\n"</span><span class="token punctuation">,</span> <span class="token function">getcount</span><span class="token punctuation">(</span><span class="token string">"test"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">"pwd"</span><span class="token punctuation">,</span> O_RDONLY<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">printf</span><span class="token punctuation">(</span><span class="token constant">stdout</span><span class="token punctuation">,</span> <span class="token string">"pwd count = %d\n"</span><span class="token punctuation">,</span> <span class="token function">getcount</span><span class="token punctuation">(</span><span class="token string">"pwd"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">"666"</span><span class="token punctuation">,</span> O_RDONLY<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">printf</span><span class="token punctuation">(</span><span class="token constant">stdout</span><span class="token punctuation">,</span> <span class="token string">"666 count = %d\n"</span><span class="token punctuation">,</span> <span class="token function">getcount</span><span class="token punctuation">(</span><span class="token string">"666"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token function">printf</span><span class="token punctuation">(</span><span class="token constant">stdout</span><span class="token punctuation">,</span> <span class="token string">"test finished\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token function">exit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>从上面的代码中其实就能够看出来，<code>trace</code> 和 <code>getcount</code>虽然说是两个系统调用，<strong>可是在用户程序的视角看来，系统调用的外貌和函数没有什么两样</strong>。故</p>
<blockquote>
<p><strong>From the perspective of user program, system call are just like function calls</strong></p>
</blockquote>
<p>虽然在外形、使用上系统调用和和我们自己定义的函数差不多，但是我们需要知道，我们调用自己写的函数的时候，所有的功能其实都是我们自己写的。但是对于系统调用来说，功能都是由操作系统提供的，即：</p>
<blockquote>
<p>By using these <strong>special function (System Call)</strong>, the user program can <strong>leverage functionalities offered by the kernel</strong></p>
</blockquote>
<h4 id="Kernel’s-Operating-System’s-Perspective"><a href="#Kernel’s-Operating-System’s-Perspective" class="headerlink" title="Kernel’s (Operating System’s) Perspective"></a>Kernel’s (Operating System’s) Perspective</h4><p>我们上面讲解了我们程序员在写代码时候所看到的系统调用的样子，接下来看看操作系统眼中的系统调用是什么样子。</p>
<p>我们以<code>kill</code>这个系统调用为例进行讲解。上面说到，系统调用的功能由操作系统提供，因此<code>kill</code>的函数定义在内核的源代码<code>proc.c</code>中</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// proc.c</span>

<span class="token comment" spellcheck="true">// Kill the process with the given pid.</span>
<span class="token comment" spellcheck="true">// Process won't exit until it returns</span>
<span class="token comment" spellcheck="true">// to user space (see trap in trap.c).</span>
<span class="token keyword">int</span>
<span class="token function">kill</span><span class="token punctuation">(</span><span class="token keyword">int</span> pid<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
  <span class="token keyword">struct</span> proc <span class="token operator">*</span>p<span class="token punctuation">;</span>

  <span class="token function">acquire</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ptable<span class="token punctuation">.</span>lock<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">for</span><span class="token punctuation">(</span>p <span class="token operator">=</span> ptable<span class="token punctuation">.</span>proc<span class="token punctuation">;</span> p <span class="token operator">&lt;</span> <span class="token operator">&amp;</span>ptable<span class="token punctuation">.</span>proc<span class="token punctuation">[</span>NPROC<span class="token punctuation">]</span><span class="token punctuation">;</span> p<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>p<span class="token operator">-></span>pid <span class="token operator">==</span> pid<span class="token punctuation">)</span><span class="token punctuation">{</span>
      p<span class="token operator">-></span>killed <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
      <span class="token comment" spellcheck="true">// Wake process from sleep if necessary.</span>
      <span class="token keyword">if</span><span class="token punctuation">(</span>p<span class="token operator">-></span>state <span class="token operator">==</span> SLEEPING<span class="token punctuation">)</span>
        p<span class="token operator">-></span>state <span class="token operator">=</span> RUNNABLE<span class="token punctuation">;</span>
      <span class="token function">release</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ptable<span class="token punctuation">.</span>lock<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token function">release</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ptable<span class="token punctuation">.</span>lock<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>所以，<strong>其实站在内核的角度，所谓的系统调用只不过是内核程序中定义的函数而已</strong>，即：</p>
<blockquote>
<p><strong>So, from the perspective of kernel, system call are just normal functions defined in kernel.</strong></p>
</blockquote>
<p>可是，这样就存在一个很关键的问题：<strong>我们在上面说过，操作系统和用户程序都是独立的程序。而我们在编译的时候是分别编译的用户程序和内核，我们并没有将两者链接到一起。但是我们却能够在用户程序中，调用在操作系统这个独立的程序中定义的函数。</strong></p>
<h3 id="C-从用户程序到内核"><a href="#C-从用户程序到内核" class="headerlink" title="C. 从用户程序到内核"></a>C. 从用户程序到内核</h3><p>为了解决上面讲的我们，我们接下来讲解从用户程序到内核的全过程。</p>
<h4 id="1-usys-S"><a href="#1-usys-S" class="headerlink" title="1) usys.S "></a>1) <span id="scn"><code>usys.S</code> </span></h4><p>System call的第一步就是<code>usys.S</code>这个程序。</p>
<p><code>usys.S</code>是一个<code>AT&amp;T</code>格式的汇编程序，其中主要定义了一个宏函数<code>SYSCALL</code></p>
<pre class="line-numbers language-assembly"><code class="language-assembly">#include "syscall.h"
#include "traps.h"

#define SYSCALL(name) \
  .globl name; \
  name: \
    movl $SYS_ ## name, %eax; \
    int $T_SYSCALL; \
    ret

SYSCALL(fork)
SYSCALL(exit)
SYSCALL(wait)
SYSCALL(pipe)
SYSCALL(read)
SYSCALL(write)
SYSCALL(close)
SYSCALL(kill)
SYSCALL(exec)
SYSCALL(open)
SYSCALL(mknod)
SYSCALL(unlink)
SYSCALL(fstat)
SYSCALL(link)
SYSCALL(mkdir)
SYSCALL(chdir)
SYSCALL(dup)
SYSCALL(getpid)
SYSCALL(sbrk)
SYSCALL(sleep)
SYSCALL(uptime)
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>汇编中<code>##</code>是字符拼接操作符， 所以如果我们把<code>fork</code>作为<code>SYSCALL</code>的参数传入的话，宏函数的内容就会像下面这样</p>
<pre class="line-numbers language-assembly"><code class="language-assembly">; SYSCALL(fork) will expand as

#define SYSCALL(fork) \
  .globl fork; \
  fork: \
    movl $SYS_fork, %eax; \
    int $T_SYSCALL; \
    ret
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>而<code>$SYS_call</code>这个符号变量定义在 <span id="anchor"> <code>syscall.h</code> </span>中</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// syscall.h</span>

<span class="token comment" spellcheck="true">// System call numbers</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_fork    1</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_exit    2</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_wait    3</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_pipe    4</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_read    5</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_kill    6</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_exec    7</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_fstat   8</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_chdir   9</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_dup    10</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_getpid 11</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_sbrk   12</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_sleep  13</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_uptime 14</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_open   15</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_write  16</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_mknod  17</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_unlink 18</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_link   19</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_mkdir  20</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_close  21</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>而符号变量<code>T_SYSCALL</code>的值是<code>64</code>，定义在<code>traps.h</code>中</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// These are arbitrarily chosen, but with care not to overlap</span>
<span class="token comment" spellcheck="true">// processor defined exceptions or interrupt vectors.</span>
<span class="token macro property">#<span class="token directive keyword">define</span> T_SYSCALL       64      </span><span class="token comment" spellcheck="true">// system call</span>
<span class="token macro property">#<span class="token directive keyword">define</span> T_DEFAULT      500      </span><span class="token comment" spellcheck="true">// catchall</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<p>总的来说，宏函数<code>SYSCALL</code>干的是就是把系统调用号（System Call Number）存到<code>eax</code>寄存器中，而后使用软中断指令<code>int</code>调用<code>64</code>号中断。</p>
<p>In summary, the macro function <code>SYSCALL</code> defines a assembly function that store the system call number into <code>eax</code> register and then call #64 interrput.</p>
<p>And <code>usys.S</code> simply defines assembly functions for all system call.</p>
<p>因此，<code>usys.S</code>这个程序中其实就是为每个系统调用定义了一个汇编函数，汇编函数的作用就是把系统调用号放入<code>eax</code>中</p>
<h4 id="2-vector-S"><a href="#2-vector-S" class="headerlink" title="2) vector.S"></a>2) <code>vector.S</code></h4><p>系统调用的下一步就是在<code>vector.S</code>中。<code>vector.S</code>中定义了所有的中断处理函数。</p>
<p><code>SYSCALL</code>宏的最后跳转到了<code>64</code>号中断，<code>64</code>号中断的定义如下</p>
<pre class="line-numbers language-assembly"><code class="language-assembly">.globl vector64
vector64:
  pushl $0
  pushl $64
  jmp alltraps
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><span id="trapno"><code>AT&amp;T</code>格式的汇编中，立即数前要加<code>$</code>，因此<code>64</code>号中断的内容就是先把<code>0</code>压入栈，然后再把<code>64</code>压入栈。最后再跳转到<code>alltraps</code>函数中</span></p>
<h4 id="3-trapasm-S"><a href="#3-trapasm-S" class="headerlink" title="3) trapasm.S"></a>3) <code>trapasm.S</code></h4><p><code>alltraps</code>是一个汇编函数，定义在<code>trapasm.S</code>中。<code>alltraps</code>函数的定义如下</p>
<pre class="line-numbers language-assembly"><code class="language-assembly">.globl alltraps
alltraps:
  # Build trap frame.
  pushl %ds
  pushl %es
  pushl %fs
  pushl %gs
  pushal

  # Set up data segments.
  movw $(SEG_KDATA<<3), %ax
  movw %ax, %ds
  movw %ax, %es

  # Call trap(tf), where tf=%esp
  pushl %esp
  call trap
  addl $4, %esp
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><code>alltraps</code>函数干了三件事情：</p>
<ul>
<li>首先把<code>ds</code>、<code>es</code>、<code>fs</code>和<code>gs</code>等段寄存器的值压入栈中，然后再把通用寄存器的值压入栈中</li>
<li>重新设置数据为内核数据段</li>
<li>调用<code>trap</code>函数</li>
</ul>
<h4 id="4-trap-c"><a href="#4-trap-c" class="headerlink" title="4) trap.c"></a>4) <code>trap.c</code></h4><p><code>trap</code>是一个C语言函数，定义在<code>trap.c</code>中。</p>
<p>这里我们首先需要关注的是：</p>
<blockquote>
<p><code>trap</code>函数接受一个<code>struct trapframe *</code>类型的参数<code>tf</code>。</p>
<p>C语言间相互调用函数时候传递参数是非常简单的，例如我们在<code>my_trap_call_test.c</code>文件中想要调用<code>trap</code>函数的话，直接以<code>trap(mytf)</code>这样的形式调用即可。</p>
<p>可问题是，<code>trap</code>这个C语言函数是被<code>trapasm.S</code>汇编程序中<code>alltraps</code>汇编函数调用的。所以关键问题就在于：<strong>汇编语言调用C语言函数的时候，该如何传参？</strong></p>
</blockquote>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">//PAGEBREAK: 41</span>
<span class="token keyword">void</span>
<span class="token function">trap</span><span class="token punctuation">(</span><span class="token keyword">struct</span> trapframe <span class="token operator">*</span>tf<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
  <span class="token keyword">if</span><span class="token punctuation">(</span>tf<span class="token operator">-></span>trapno <span class="token operator">==</span> T_SYSCALL<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">myproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span>killed<span class="token punctuation">)</span>
      <span class="token function">exit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">myproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span>tf <span class="token operator">=</span> tf<span class="token punctuation">;</span>
    <span class="token function">syscall</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">myproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span>killed<span class="token punctuation">)</span>
      <span class="token function">exit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">switch</span><span class="token punctuation">(</span>tf<span class="token operator">-></span>trapno<span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token keyword">case</span> T_IRQ0 <span class="token operator">+</span> IRQ_TIMER<span class="token punctuation">:</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">cpuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token function">acquire</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>tickslock<span class="token punctuation">)</span><span class="token punctuation">;</span>
      ticks<span class="token operator">++</span><span class="token punctuation">;</span>
      <span class="token function">wakeup</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ticks<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token function">release</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>tickslock<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">lapiceoi</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">break</span><span class="token punctuation">;</span>
  <span class="token keyword">case</span> T_IRQ0 <span class="token operator">+</span> IRQ_IDE<span class="token punctuation">:</span>
    <span class="token function">ideintr</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">lapiceoi</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">break</span><span class="token punctuation">;</span>
  <span class="token keyword">case</span> T_IRQ0 <span class="token operator">+</span> IRQ_IDE<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">:</span>
    <span class="token comment" spellcheck="true">// Bochs generates spurious IDE1 interrupts.</span>
    <span class="token keyword">break</span><span class="token punctuation">;</span>
  <span class="token keyword">case</span> T_IRQ0 <span class="token operator">+</span> IRQ_KBD<span class="token punctuation">:</span>
    <span class="token function">kbdintr</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">lapiceoi</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">break</span><span class="token punctuation">;</span>
  <span class="token keyword">case</span> T_IRQ0 <span class="token operator">+</span> IRQ_COM1<span class="token punctuation">:</span>
    <span class="token function">uartintr</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">lapiceoi</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">break</span><span class="token punctuation">;</span>
  <span class="token keyword">case</span> T_IRQ0 <span class="token operator">+</span> <span class="token number">7</span><span class="token punctuation">:</span>
  <span class="token keyword">case</span> T_IRQ0 <span class="token operator">+</span> IRQ_SPURIOUS<span class="token punctuation">:</span>
    <span class="token function">cprintf</span><span class="token punctuation">(</span><span class="token string">"cpu%d: spurious interrupt at %x:%x\n"</span><span class="token punctuation">,</span>
            <span class="token function">cpuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> tf<span class="token operator">-></span>cs<span class="token punctuation">,</span> tf<span class="token operator">-></span>eip<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">lapiceoi</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">break</span><span class="token punctuation">;</span>

  <span class="token comment" spellcheck="true">//PAGEBREAK: 13</span>
  <span class="token keyword">default</span><span class="token punctuation">:</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">myproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> <span class="token punctuation">(</span>tf<span class="token operator">-></span>cs<span class="token operator">&amp;</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token comment" spellcheck="true">// In kernel, it must be our mistake.</span>
      <span class="token function">cprintf</span><span class="token punctuation">(</span><span class="token string">"unexpected trap %d from cpu %d eip %x (cr2=0x%x)\n"</span><span class="token punctuation">,</span>
              tf<span class="token operator">-></span>trapno<span class="token punctuation">,</span> <span class="token function">cpuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> tf<span class="token operator">-></span>eip<span class="token punctuation">,</span> <span class="token function">rcr2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token function">panic</span><span class="token punctuation">(</span><span class="token string">"trap"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment" spellcheck="true">// In user space, assume process misbehaved.</span>
    <span class="token function">cprintf</span><span class="token punctuation">(</span><span class="token string">"pid %d %s: trap %d err %d on cpu %d "</span>
            <span class="token string">"eip 0x%x addr 0x%x--kill proc\n"</span><span class="token punctuation">,</span>
            <span class="token function">myproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span>pid<span class="token punctuation">,</span> <span class="token function">myproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span>name<span class="token punctuation">,</span> tf<span class="token operator">-></span>trapno<span class="token punctuation">,</span>
            tf<span class="token operator">-></span>err<span class="token punctuation">,</span> <span class="token function">cpuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> tf<span class="token operator">-></span>eip<span class="token punctuation">,</span> <span class="token function">rcr2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">myproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span>killed <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><strong>解决汇编传参给C语言函数的关键就是使用栈</strong>，即：</p>
<ul>
<li><strong>当我们在C语言间调用函数的时候，编译器会自动帮我们把主调函数中的参数值压入栈中</strong></li>
<li><strong>而函数调用本质上是通过<code>jmp</code>指令实现的，因此<code>sp</code>和<code>ss</code>这两个保存了栈顶和栈底的寄存器的值是没有改变的</strong></li>
<li><strong>因此，在被调函数中通过出栈就可以获得主调函数中传递的参数值</strong></li>
</ul>
<p>通过栈这种方式，C语言实现了主调函数为被调函数中的形参赋值。而编译器则帮助我们完成了压栈和出栈的过程。</p>
<p>因此，现在我们在汇编中调用C语言函数的话，我们需要自己把参数全部压入到栈中。由于压栈和出栈的顺序是反过来的，所以我们需要去看一下<code>trapframe</code>这个结构体的内容。</p>
<p><code>trapframe</code>的定义和<code>alltraps</code>压栈的顺序如下：</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// definition of trapframe in x86.h</span>

<span class="token comment" spellcheck="true">//PAGEBREAK: 36</span>
<span class="token comment" spellcheck="true">// Layout of the trap frame built on the stack by the</span>
<span class="token comment" spellcheck="true">// hardware and by trapasm.S, and passed to trap().</span>
<span class="token keyword">struct</span> trapframe <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// registers as pushed by pusha</span>
  uint edi<span class="token punctuation">;</span>
  uint esi<span class="token punctuation">;</span>
  uint ebp<span class="token punctuation">;</span>
  uint oesp<span class="token punctuation">;</span>      <span class="token comment" spellcheck="true">// useless &amp; ignored</span>
  uint ebx<span class="token punctuation">;</span>
  uint edx<span class="token punctuation">;</span>
  uint ecx<span class="token punctuation">;</span>
  uint eax<span class="token punctuation">;</span>

  <span class="token comment" spellcheck="true">// rest of trap frame</span>
  ushort gs<span class="token punctuation">;</span>
  ushort padding1<span class="token punctuation">;</span>
  ushort fs<span class="token punctuation">;</span>
  ushort padding2<span class="token punctuation">;</span>
  ushort es<span class="token punctuation">;</span>
  ushort padding3<span class="token punctuation">;</span>
  ushort ds<span class="token punctuation">;</span>
  ushort padding4<span class="token punctuation">;</span>
  uint trapno<span class="token punctuation">;</span>

  <span class="token comment" spellcheck="true">// below here defined by x86 hardware</span>
  uint err<span class="token punctuation">;</span>
  uint eip<span class="token punctuation">;</span>
  ushort cs<span class="token punctuation">;</span>
  ushort padding5<span class="token punctuation">;</span>
  uint eflags<span class="token punctuation">;</span>

  <span class="token comment" spellcheck="true">// below here only when crossing rings, such as from user to kernel</span>
  uint esp<span class="token punctuation">;</span>
  ushort ss<span class="token punctuation">;</span>
  ushort padding6<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>


<span class="token comment" spellcheck="true">// push in alltraps</span>
<span class="token punctuation">.</span>globl alltraps
alltraps<span class="token punctuation">:</span>
  <span class="token macro property"># Build trap frame.</span>
  pushl <span class="token operator">%</span>ds
  pushl <span class="token operator">%</span>es
  pushl <span class="token operator">%</span>fs
  pushl <span class="token operator">%</span>gs
  pushal

  <span class="token macro property"># Set up data segments.</span>
  movw $<span class="token punctuation">(</span>SEG_KDATA<span class="token operator">&lt;&lt;</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">%</span>ax
  movw <span class="token operator">%</span>ax<span class="token punctuation">,</span> <span class="token operator">%</span>ds
  movw <span class="token operator">%</span>ax<span class="token punctuation">,</span> <span class="token operator">%</span>es

  <span class="token macro property"># Call trap(tf), where tf=%esp</span>
  pushl <span class="token operator">%</span>esp
  call trap
  addl $<span class="token number">4</span><span class="token punctuation">,</span> <span class="token operator">%</span>esp
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>我们可以看到对应如下：</p>
<ul>
<li><code>alltraps</code>压栈的内容和<code>trapframe</code>中25行以前的内容一一对应。</li>
<li>26行的<code>trapno</code>是在<a href=""><code>vector.S</code></a>中压栈的，其值就是中断号的值，即<code>64</code>号中断</li>
</ul>
<p>最关键的是，<strong>目前我们的程序已经运行到了<code>trap.c</code>中，而<code>trap.c</code>是内核程序的一部分，换而言之，我们此刻正在执行内核代码</strong>，即：</p>
<ol>
<li>我们已经进入了内核态</li>
<li>我们从用户态跳转到了内核态</li>
</ol>
<p>因此，<code>操作系统使用三个汇编语言函数来实现从用户程序跳转到内核程序中，从而使得程序A中运行定义在程序B中的函数称为可能。而实现跳转的关键，就是通过中断</code></p>
<p>话说回来，上面我们说到，运行到<code>trap</code>函数其实就已经在执行内核代码。</p>
<p>我们精简一下<code>trap</code>函数，主要看看其中的逻辑。很明显，就是根据<code>trapno</code>来判断要执行那些动作：</p>
<ul>
<li>时钟中断发生了之后，调用操作系统的Scheduler来调度下一个程序</li>
<li>IO中断发生了之后，调用驱动和硬件交互</li>
<li>处理系统调用中断</li>
</ul>
<p>因此<strong><code>trap</code>函数里面的逻辑主要就是根据不同的中断号来执行不同的代码</strong>。</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">//PAGEBREAK: 41</span>
<span class="token keyword">void</span>
<span class="token function">trap</span><span class="token punctuation">(</span><span class="token keyword">struct</span> trapframe <span class="token operator">*</span>tf<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
  <span class="token keyword">if</span><span class="token punctuation">(</span>tf<span class="token operator">-></span>trapno <span class="token operator">==</span> T_SYSCALL<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">switch</span><span class="token punctuation">(</span>tf<span class="token operator">-></span>trapno<span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token keyword">case</span> T_IRQ0 <span class="token operator">+</span> IRQ_TIMER<span class="token punctuation">:</span>
        <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
      <span class="token keyword">break</span><span class="token punctuation">;</span>

    <span class="token keyword">case</span> T_IRQ0 <span class="token operator">+</span> IRQ_IDE<span class="token punctuation">:</span>
        <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
      <span class="token keyword">break</span><span class="token punctuation">;</span>

    <span class="token keyword">case</span> T_IRQ0 <span class="token operator">+</span> IRQ_IDE<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">:</span>
        <span class="token comment" spellcheck="true">// Bochs generates spurious IDE1 interrupts.</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>

    <span class="token keyword">case</span> T_IRQ0 <span class="token operator">+</span> IRQ_KBD<span class="token punctuation">:</span>
        <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>

    <span class="token keyword">case</span> T_IRQ0 <span class="token operator">+</span> IRQ_COM1<span class="token punctuation">:</span>
            <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
      <span class="token keyword">break</span><span class="token punctuation">;</span>

    <span class="token keyword">case</span> T_IRQ0 <span class="token operator">+</span> <span class="token number">7</span><span class="token punctuation">:</span>
      <span class="token keyword">case</span> T_IRQ0 <span class="token operator">+</span> IRQ_SPURIOUS<span class="token punctuation">:</span>
        <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
      <span class="token keyword">break</span><span class="token punctuation">;</span>

      <span class="token comment" spellcheck="true">//PAGEBREAK: 13</span>
      <span class="token keyword">default</span><span class="token punctuation">:</span>
        <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>我们这里因为关注如何给<code>XV6</code>增加一个系统调用，所以我们其实真正关注的内容就是第一个<code>if</code>中的内容，即</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token keyword">if</span> <span class="token punctuation">(</span>tf<span class="token operator">-></span>trapno <span class="token operator">==</span> T_SYSCALL<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">myproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span>killed<span class="token punctuation">)</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">myproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span>tf <span class="token operator">=</span> tf<span class="token punctuation">;</span>
    <span class="token function">syscall</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">myproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span>killed<span class="token punctuation">)</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>我们在上面说过<code>trapno</code>中的值是在<a href="#trapno"><code>vector.S</code></a>中压入栈的，而<code>vector64</code>就将<code>trapno</code>的值设置为了<code>64</code>，和<code>T_SYSCALL</code>的值一样。</p>
<p>因此，操作系统就明白了此时发生了系统调用，于是就调用<code>syscall</code>去处理系统调用</p>
<h3 id="D-Syscall函数"><a href="#D-Syscall函数" class="headerlink" title="D. Syscall函数"></a>D. Syscall函数</h3><p>接下来，我们关注<code>syscall</code>函数。<code>syscall</code>函数是一个C语言函数，定义在<code>syscall.c</code>中。</p>
<p>相关的代码如下</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// syscall.c</span>

<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_chdir</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_close</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_dup</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_exec</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_exit</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_fork</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_fstat</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_getpid</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_kill</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_link</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_mkdir</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_mknod</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_open</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_pipe</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_read</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_sbrk</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_sleep</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_unlink</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_wait</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_write</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_uptime</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token punctuation">(</span><span class="token operator">*</span>syscalls<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
<span class="token punctuation">[</span>SYS_fork<span class="token punctuation">]</span>    sys_fork<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_exit<span class="token punctuation">]</span>    sys_exit<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_wait<span class="token punctuation">]</span>    sys_wait<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_pipe<span class="token punctuation">]</span>    sys_pipe<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_read<span class="token punctuation">]</span>    sys_read<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_kill<span class="token punctuation">]</span>    sys_kill<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_exec<span class="token punctuation">]</span>    sys_exec<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_fstat<span class="token punctuation">]</span>   sys_fstat<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_chdir<span class="token punctuation">]</span>   sys_chdir<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_dup<span class="token punctuation">]</span>     sys_dup<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_getpid<span class="token punctuation">]</span>  sys_getpid<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_sbrk<span class="token punctuation">]</span>    sys_sbrk<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_sleep<span class="token punctuation">]</span>   sys_sleep<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_uptime<span class="token punctuation">]</span>  sys_uptime<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_open<span class="token punctuation">]</span>    sys_open<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_write<span class="token punctuation">]</span>   sys_write<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_mknod<span class="token punctuation">]</span>   sys_mknod<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_unlink<span class="token punctuation">]</span>  sys_unlink<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_link<span class="token punctuation">]</span>    sys_link<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_mkdir<span class="token punctuation">]</span>   sys_mkdir<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_close<span class="token punctuation">]</span>   sys_close<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">void</span>
<span class="token function">syscall</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
  <span class="token keyword">int</span> num<span class="token punctuation">;</span>
  <span class="token keyword">struct</span> proc <span class="token operator">*</span>curproc <span class="token operator">=</span> <span class="token function">myproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  num <span class="token operator">=</span> curproc<span class="token operator">-></span>tf<span class="token operator">-></span>eax<span class="token punctuation">;</span>
  <span class="token keyword">if</span><span class="token punctuation">(</span>num <span class="token operator">></span> <span class="token number">0</span> <span class="token operator">&amp;&amp;</span> num <span class="token operator">&lt;</span> <span class="token function">NELEM</span><span class="token punctuation">(</span>syscalls<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> syscalls<span class="token punctuation">[</span>num<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    curproc<span class="token operator">-></span>tf<span class="token operator">-></span>eax <span class="token operator">=</span> syscalls<span class="token punctuation">[</span>num<span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token function">cprintf</span><span class="token punctuation">(</span><span class="token string">"%d %s: unknown sys call %d\n"</span><span class="token punctuation">,</span>
            curproc<span class="token operator">-></span>pid<span class="token punctuation">,</span> curproc<span class="token operator">-></span>name<span class="token punctuation">,</span> num<span class="token punctuation">)</span><span class="token punctuation">;</span>
    curproc<span class="token operator">-></span>tf<span class="token operator">-></span>eax <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>其中，比较关键的如下：</p>
<ul>
<li>变量 <code>syscalls</code>是一个函数指针数组，即数组中的每一个元素都是一个指向函数的指针。 而<code>SYS_xxx</code>这些宏常量的值都定义在<code>syscall.h</code>中 (参考 <a href="#anchor">syscall.h</a>)</li>
<li>此外，系统调用号（Syatem Call Number）存储在<code>eax</code>中 (参考 <a href="#scn">usys.S</a>).</li>
</ul>
<p>因此，<code>syscall</code>函数首先通过当前进程的<code>eax</code>寄存器获得系统调用号（line #55），然后根据系统调用号去从<code>syscalls</code>函数数组中选择对应的函数去执行（line</p>
<h1 id="57）"><a href="#57）" class="headerlink" title="57）"></a>57）</h1><p>需要注意的是，因为函数数组中的函数都是在别的文件中定义的，所以需要在最前面使用 <code>extern</code>关键字去声明一下外部符号常量，例如：<code>sys_exit</code>、<code>sys_close</code>和 <code>sys_mkdir</code></p>
<h3 id="E-sys-XXX系统调用"><a href="#E-sys-XXX系统调用" class="headerlink" title="E. sys_XXX系统调用"></a>E. sys_XXX系统调用</h3><p>最后，我们看一下<code>sys_xxx</code>之类的系统调用的定义（以<code>sys_kill</code>为例）</p>
<p><code>sys_kill</code>定义在<code>sysproc.c</code>中</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// sysproc.c</span>
<span class="token keyword">int</span>
<span class="token function">sys_kill</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
  <span class="token keyword">int</span> pid<span class="token punctuation">;</span>

  <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">argint</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>pid<span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> <span class="token function">kill</span><span class="token punctuation">(</span>pid<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>而<code>kill</code>则定义在在<code>proc.c</code>中。</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// proc.c</span>

<span class="token comment" spellcheck="true">// Kill the process with the given pid.</span>
<span class="token comment" spellcheck="true">// Process won't exit until it returns</span>
<span class="token comment" spellcheck="true">// to user space (see trap in trap.c).</span>
<span class="token keyword">int</span>
<span class="token function">kill</span><span class="token punctuation">(</span><span class="token keyword">int</span> pid<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
  <span class="token keyword">struct</span> proc <span class="token operator">*</span>p<span class="token punctuation">;</span>

  <span class="token function">acquire</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ptable<span class="token punctuation">.</span>lock<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">for</span><span class="token punctuation">(</span>p <span class="token operator">=</span> ptable<span class="token punctuation">.</span>proc<span class="token punctuation">;</span> p <span class="token operator">&lt;</span> <span class="token operator">&amp;</span>ptable<span class="token punctuation">.</span>proc<span class="token punctuation">[</span>NPROC<span class="token punctuation">]</span><span class="token punctuation">;</span> p<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>p<span class="token operator">-></span>pid <span class="token operator">==</span> pid<span class="token punctuation">)</span><span class="token punctuation">{</span>
      p<span class="token operator">-></span>killed <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
      <span class="token comment" spellcheck="true">// Wake process from sleep if necessary.</span>
      <span class="token keyword">if</span><span class="token punctuation">(</span>p<span class="token operator">-></span>state <span class="token operator">==</span> SLEEPING<span class="token punctuation">)</span>
        p<span class="token operator">-></span>state <span class="token operator">=</span> RUNNABLE<span class="token punctuation">;</span>
      <span class="token function">release</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ptable<span class="token punctuation">.</span>lock<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token function">release</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ptable<span class="token punctuation">.</span>lock<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="F-传递参数"><a href="#F-传递参数" class="headerlink" title="F. 传递参数"></a>F. 传递参数</h3><p>在<code>XV6</code>的实现中，<code>syscall</code>中使用函数数组就是为了方便未来扩展系统调用，这样未来我们如果自己想要添加系统调用的话，直接给数组中新增加函数就行了。可是这样做又会造成新的问题，即参数传递问题。</p>
<p>用户想要使用系统调用的话，需要include <code>user.h</code>这个头文件，其中提供了所有系统调用的函数原型</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// user.h</span>

<span class="token comment" spellcheck="true">// system calls</span>
<span class="token keyword">int</span> <span class="token function">fork</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">exit</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span> <span class="token function">__attribute__</span><span class="token punctuation">(</span><span class="token punctuation">(</span>noreturn<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">wait</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">pipe</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">write</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">const</span> <span class="token keyword">void</span><span class="token operator">*</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">void</span><span class="token operator">*</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">close</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">kill</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">exec</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">,</span> <span class="token keyword">char</span><span class="token operator">*</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">open</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">mknod</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">,</span> <span class="token keyword">short</span><span class="token punctuation">,</span> <span class="token keyword">short</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">unlink</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">fstat</span><span class="token punctuation">(</span><span class="token keyword">int</span> fd<span class="token punctuation">,</span> <span class="token keyword">struct</span> stat<span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">link</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">,</span> <span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">mkdir</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">chdir</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">dup</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">getpid</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">char</span><span class="token operator">*</span> <span class="token function">sbrk</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">sleep</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">uptime</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>可以有一个问题，我们上面在<code>syscall.c</code>的<code>syscalls</code>函数数组中所有的函数都是<code>int xxxx(void)</code>，即所有的函数都是不接受参数的（参考 <a href="#sc">syscall.c</a>）</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_chdir</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_close</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_dup</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_exec</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_exit</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_fork</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_fstat</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_getpid</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_kill</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_link</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_mkdir</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_mknod</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_open</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_pipe</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_read</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_sbrk</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_sleep</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_unlink</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_wait</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_write</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_uptime</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>但是很多系统调用都是需要参数的，例如<code>kill</code>就需要进程的PID。因此，为了解决参数传递问题，<code>XV6</code>中又提供了新的机制。</p>
<p>前面解释过，C语言通过栈传递参数。而我们C语言再声明函数的时候，通过<code>int xxx(char a, int b)</code>来通知编译器，主调函数调用函数前把一个<code>char</code>类型的数据（一般是一个字节）先压入栈中，而后再把一个<code>int</code>类型的数据（一般是四个字节）压入栈中。而后进入被调函数之后就从栈中弹出五个字节即可。</p>
<p><strong>因此，我们其实在知道的了一个函数接受的所有的参数的数据类型的情况下，我们其实可以手动从栈中去读取数据</strong></p>
<p>而<code>XV6</code>中提供了两个函数去从当前栈（中间<code>sp</code>和<code>ss</code>一直没变）中获取参数。</p>
<p>这两个函数分别是<code>argstr</code>和<code>argint</code>，定义在<code>syscall.c</code>中。</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// Fetch the int at addr from the current process.</span>
<span class="token keyword">int</span>
<span class="token function">fetchint</span><span class="token punctuation">(</span>uint addr<span class="token punctuation">,</span> <span class="token keyword">int</span> <span class="token operator">*</span>ip<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
  <span class="token keyword">struct</span> proc <span class="token operator">*</span>curproc <span class="token operator">=</span> <span class="token function">myproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">if</span><span class="token punctuation">(</span>addr <span class="token operator">>=</span> curproc<span class="token operator">-></span>sz <span class="token operator">||</span> addr<span class="token operator">+</span><span class="token number">4</span> <span class="token operator">></span> curproc<span class="token operator">-></span>sz<span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
  <span class="token operator">*</span>ip <span class="token operator">=</span> <span class="token operator">*</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">(</span>addr<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// Fetch the nul-terminated string at addr from the current process.</span>
<span class="token comment" spellcheck="true">// Doesn't actually copy the string - just sets *pp to point at it.</span>
<span class="token comment" spellcheck="true">// Returns length of string, not including nul.</span>
<span class="token keyword">int</span>
<span class="token function">fetchstr</span><span class="token punctuation">(</span>uint addr<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>pp<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
  <span class="token keyword">char</span> <span class="token operator">*</span>s<span class="token punctuation">,</span> <span class="token operator">*</span>ep<span class="token punctuation">;</span>
  <span class="token keyword">struct</span> proc <span class="token operator">*</span>curproc <span class="token operator">=</span> <span class="token function">myproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">if</span><span class="token punctuation">(</span>addr <span class="token operator">>=</span> curproc<span class="token operator">-></span>sz<span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
  <span class="token operator">*</span>pp <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span>addr<span class="token punctuation">;</span>
  ep <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span>curproc<span class="token operator">-></span>sz<span class="token punctuation">;</span>
  <span class="token keyword">for</span><span class="token punctuation">(</span>s <span class="token operator">=</span> <span class="token operator">*</span>pp<span class="token punctuation">;</span> s <span class="token operator">&lt;</span> ep<span class="token punctuation">;</span> s<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">*</span>s <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span>
      <span class="token keyword">return</span> s <span class="token operator">-</span> <span class="token operator">*</span>pp<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// Fetch the nth 32-bit system call argument.</span>
<span class="token keyword">int</span>
<span class="token function">argint</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> <span class="token keyword">int</span> <span class="token operator">*</span>ip<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token function">fetchint</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token function">myproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span>tf<span class="token operator">-></span>esp<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">4</span> <span class="token operator">+</span> <span class="token number">4</span><span class="token operator">*</span>n<span class="token punctuation">,</span> ip<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// Fetch the nth word-sized system call argument as a string pointer.</span>
<span class="token comment" spellcheck="true">// Check that the pointer is valid and the string is nul-terminated.</span>
<span class="token comment" spellcheck="true">// (There is no shared writable memory, so the string can't change</span>
<span class="token comment" spellcheck="true">// between this check and being used by the kernel.)</span>
<span class="token keyword">int</span>
<span class="token function">argstr</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>pp<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
  <span class="token keyword">int</span> addr<span class="token punctuation">;</span>
  <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">argint</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token operator">&amp;</span>addr<span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> <span class="token function">fetchstr</span><span class="token punctuation">(</span>addr<span class="token punctuation">,</span> pp<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="argint和fetchint函数"><a href="#argint和fetchint函数" class="headerlink" title="argint和fetchint函数"></a>argint和fetchint函数</h4><p><code>argint</code>函数用于从程序的栈中获取第<code>n</code>个<code>int</code>数据。而<code>fetchint</code>则是给定一个地址，而后在进行完地址检查之后，从该地址中取一个<code>int</code>出来。</p>
<p>因此<code>argint</code>中计算完偏移量之后，直接把<code>程序当前的栈顶地址+偏移量</code>传给<code>fectint</code>函数。而<code>fetchint</code>函数中直接进行的值复制，因此最后传入<code>argint</code>的<code>ip</code>（integer· pointer）就储存了整数值的拷贝。</p>
<p>而在<code>argstr</code>函数中，首先调用了<code>argint</code>。而调用<code>argint</code>的目的是：</p>
<ul>
<li>利用<code>argint</code>计算偏移地址，从而得到字符串的开头地址，而后通过<code>fetchint</code>检查字符串的开头地址是否非法（地址高于栈底地址）。</li>
</ul>
<p>最后，<code>argstr</code>调用<code>fetchstr</code>将字符串指针<code>pp</code>指向了字符串开头的地址。</p>
<p>到此为止，我们就介绍完了<code>XV6</code>中系统调用的全部流程以及必要的函数</p>
<h2 id="3-增加trace和getcount系统调用"><a href="#3-增加trace和getcount系统调用" class="headerlink" title="3. 增加trace和getcount系统调用"></a>3. 增加<code>trace</code>和<code>getcount</code>系统调用</h2><p>接下来，我们就将添加<code>trace</code>和<code>getcount</code>这两个系统调用</p>
<h3 id="1-增加系统调用号"><a href="#1-增加系统调用号" class="headerlink" title="1. 增加系统调用号"></a>1. 增加系统调用号</h3><p><code>syscall.h</code>中定义了所有的系统调用号，而系统调用号将会在<code>syscall.c</code>和<code>usys.S</code>中用的</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// my syscall</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_trace  22</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SYS_getcount  23</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/%E6%88%AA%E5%B1%8F2022-09-26%20%E4%B8%8B%E5%8D%882.02.24.png" alt="增加两个系统调用号"></p>
<h3 id="2-增加SYSCALL宏函数"><a href="#2-增加SYSCALL宏函数" class="headerlink" title="2. 增加SYSCALL宏函数"></a>2. 增加SYSCALL宏函数</h3><p>接下来要做的，就是在<code>usys.S</code>文件中为我们的系统调用增加宏函数。</p>
<pre class="line-numbers language-assembly"><code class="language-assembly">SYSCALL(trace)
SYSCALL(getcount)
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/%E6%88%AA%E5%B1%8F2022-09-26%20%E4%B8%8B%E5%8D%882.04.52.png" alt="增加SYSCALL宏函数"></p>
<h3 id="3-syscall-c中定义函数原型"><a href="#3-syscall-c中定义函数原型" class="headerlink" title="3.  syscall.c中定义函数原型"></a>3.  <code>syscall.c</code>中定义函数原型</h3><p>接下来我们要做的就是在<code>syscall.c</code>中增加我们的函数到<code>syscalls</code>函数数组中去</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// my syscall</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_trace</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">int</span> <span class="token function">sys_getcount</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token punctuation">(</span><span class="token operator">*</span>syscalls<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
<span class="token punctuation">[</span>SYS_fork<span class="token punctuation">]</span>    sys_fork<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_exit<span class="token punctuation">]</span>    sys_exit<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_wait<span class="token punctuation">]</span>    sys_wait<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_pipe<span class="token punctuation">]</span>    sys_pipe<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_read<span class="token punctuation">]</span>    sys_read<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_kill<span class="token punctuation">]</span>    sys_kill<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_exec<span class="token punctuation">]</span>    sys_exec<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_fstat<span class="token punctuation">]</span>   sys_fstat<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_chdir<span class="token punctuation">]</span>   sys_chdir<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_dup<span class="token punctuation">]</span>     sys_dup<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_getpid<span class="token punctuation">]</span>  sys_getpid<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_sbrk<span class="token punctuation">]</span>    sys_sbrk<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_sleep<span class="token punctuation">]</span>   sys_sleep<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_uptime<span class="token punctuation">]</span>  sys_uptime<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_open<span class="token punctuation">]</span>    sys_open<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_write<span class="token punctuation">]</span>   sys_write<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_mknod<span class="token punctuation">]</span>   sys_mknod<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_unlink<span class="token punctuation">]</span>  sys_unlink<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_link<span class="token punctuation">]</span>    sys_link<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_mkdir<span class="token punctuation">]</span>   sys_mkdir<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_close<span class="token punctuation">]</span>   sys_close<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_trace<span class="token punctuation">]</span> sys_trace<span class="token punctuation">,</span>
<span class="token punctuation">[</span>SYS_getcount<span class="token punctuation">]</span> sys_getcount
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="4-实现trace和getcount"><a href="#4-实现trace和getcount" class="headerlink" title="4. 实现trace和getcount"></a>4. 实现<code>trace</code>和<code>getcount</code></h3><p>最后，我们要实现这<code>trace</code>和<code>getcount</code>这两个系统调用，因为需要和<code>open</code>系统调用配合，所以就在 <code>sysfile.c</code>中定义了</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// sysfile.c</span>

<span class="token comment" spellcheck="true">// my syscall data structure</span>
<span class="token keyword">typedef</span> <span class="token keyword">struct</span> trace_file
<span class="token punctuation">{</span>
  <span class="token keyword">char</span> filename<span class="token punctuation">[</span><span class="token number">256</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  <span class="token keyword">int</span> open_counter<span class="token punctuation">;</span>
  <span class="token keyword">int</span> trace_enabled<span class="token punctuation">;</span>
<span class="token punctuation">}</span> trace_file<span class="token punctuation">;</span>

<span class="token keyword">int</span> num_traced<span class="token punctuation">;</span>
trace_file tracing_file<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">sys_trace</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// get param from stack char *path;</span>
  <span class="token keyword">char</span> <span class="token operator">*</span>path<span class="token punctuation">;</span>
  <span class="token keyword">int</span> length <span class="token operator">=</span> <span class="token function">argstr</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>length <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>

  <span class="token keyword">int</span> k <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span>k <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> k <span class="token operator">&lt;</span> length<span class="token punctuation">;</span> k<span class="token operator">++</span><span class="token punctuation">)</span>
    tracing_file<span class="token punctuation">.</span>filename<span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">=</span> path<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">;</span>
  tracing_file<span class="token punctuation">.</span>filename<span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'\0'</span><span class="token punctuation">;</span>
tracing_file<span class="token punctuation">.</span>open_counter <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
  tracing_file<span class="token punctuation">.</span>trace_enabled <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>


<span class="token keyword">int</span> <span class="token function">sys_getcount</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>tracing_file<span class="token punctuation">.</span>trace_enabled <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> tracing_file<span class="token punctuation">.</span>open_counter<span class="token punctuation">;</span>
  <span class="token keyword">else</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>最后修改 <code>sys_open</code>使得每次打开都会增加计数。</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token keyword">int</span>
<span class="token function">sys_open</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
  <span class="token keyword">char</span> <span class="token operator">*</span>path<span class="token punctuation">;</span>
  <span class="token keyword">int</span> fd<span class="token punctuation">,</span> omode<span class="token punctuation">;</span>
  <span class="token keyword">struct</span> file <span class="token operator">*</span>f<span class="token punctuation">;</span>
  <span class="token keyword">struct</span> inode <span class="token operator">*</span>ip<span class="token punctuation">;</span>

  <span class="token comment" spellcheck="true">// check if trace enabled and add counter</span>
  <span class="token keyword">int</span> length <span class="token operator">=</span> <span class="token function">argstr</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">if</span><span class="token punctuation">(</span>length <span class="token operator">&lt;</span> <span class="token number">0</span> <span class="token operator">||</span> <span class="token function">argint</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>omode<span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>

  <span class="token keyword">char</span> <span class="token operator">*</span>ppath <span class="token operator">=</span> path<span class="token punctuation">;</span>
  <span class="token keyword">char</span> <span class="token operator">*</span>ptf <span class="token operator">=</span> tracing_file<span class="token punctuation">.</span>filename<span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">strncmp</span><span class="token punctuation">(</span>ppath<span class="token punctuation">,</span> ptf<span class="token punctuation">,</span> length<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>tracing_file<span class="token punctuation">.</span>trace_enabled<span class="token punctuation">)</span>
      tracing_file<span class="token punctuation">.</span>open_counter <span class="token operator">+</span><span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>


  <span class="token function">begin_op</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">if</span><span class="token punctuation">(</span>omode <span class="token operator">&amp;</span> O_CREATE<span class="token punctuation">)</span><span class="token punctuation">{</span>
    ip <span class="token operator">=</span> <span class="token function">create</span><span class="token punctuation">(</span>path<span class="token punctuation">,</span> T_FILE<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>ip <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token function">end_op</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>ip <span class="token operator">=</span> <span class="token function">namei</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token function">end_op</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">ilock</span><span class="token punctuation">(</span>ip<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>ip<span class="token operator">-></span>type <span class="token operator">==</span> T_DIR <span class="token operator">&amp;&amp;</span> omode <span class="token operator">!=</span> O_RDONLY<span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token function">iunlockput</span><span class="token punctuation">(</span>ip<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token function">end_op</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>f <span class="token operator">=</span> <span class="token function">filealloc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> <span class="token punctuation">(</span>fd <span class="token operator">=</span> <span class="token function">fdalloc</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span>
      <span class="token function">fileclose</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">iunlockput</span><span class="token punctuation">(</span>ip<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">end_op</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token function">iunlock</span><span class="token punctuation">(</span>ip<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">end_op</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  f<span class="token operator">-></span>type <span class="token operator">=</span> FD_INODE<span class="token punctuation">;</span>
  f<span class="token operator">-></span>ip <span class="token operator">=</span> ip<span class="token punctuation">;</span>
  f<span class="token operator">-></span>off <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
  f<span class="token operator">-></span>readable <span class="token operator">=</span> <span class="token operator">!</span><span class="token punctuation">(</span>omode <span class="token operator">&amp;</span> O_WRONLY<span class="token punctuation">)</span><span class="token punctuation">;</span>
  f<span class="token operator">-></span>writable <span class="token operator">=</span> <span class="token punctuation">(</span>omode <span class="token operator">&amp;</span> O_WRONLY<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span>omode <span class="token operator">&amp;</span> O_RDWR<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> fd<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/%E6%88%AA%E5%B1%8F2022-09-26%20%E4%B8%8B%E5%8D%882.12.01.png" alt="增加sys_trace和sys_getcount"></p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/%E6%88%AA%E5%B1%8F2022-09-26%20%E4%B8%8B%E5%8D%882.11.49.png" alt="修改sys_open"></p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">Jack Wang</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://jackwang0107.github.io/2022/10/22/xv6-zeng-jia-xi-tong-diao-yong/">https://jackwang0107.github.io/2022/10/22/xv6-zeng-jia-xi-tong-diao-yong/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">Jack Wang</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/Operating-System/">
                                    <span class="chip bg-color">Operating System</span>
                                </a>
                            
                                <a href="/tags/xv6/">
                                    <span class="chip bg-color">xv6</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.png" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.jpg" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2022/10/22/jian-shen-jiao-cheng/">
                    <div class="card-image">
                        
                        <img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221022203720074.png" class="responsive-img" alt="健身教程">
                        
                        <span class="card-title">健身教程</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            健身教程笔记，视频教程：https://space.bilibili.com/378067652/
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2022-10-22
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E5%81%A5%E8%BA%AB/" class="post-category">
                                    健身
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/%E5%81%A5%E8%BA%AB/">
                        <span class="chip bg-color">健身</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2022/10/22/xv6-xiang-mu-jie-shao/">
                    <div class="card-image">
                        
                        <img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20220928233603685.png" class="responsive-img" alt="XV6：介绍及项目结构">
                        
                        <span class="card-title">XV6：介绍及项目结构</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本文介绍了XV6，并介绍了XV6项目结构
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2022-10-22
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/xv6/" class="post-category">
                                    xv6
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Operating-System/">
                        <span class="chip bg-color">Operating System</span>
                    </a>
                    
                    <a href="/tags/xv6/">
                        <span class="chip bg-color">xv6</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>


<script>
    $('#articleContent').on('copy', function (e) {
        // IE8 or earlier browser is 'undefined'
        if (typeof window.getSelection === 'undefined') return;

        var selection = window.getSelection();
        // if the selection is short let's not annoy our users.
        if (('' + selection).length < Number.parseInt('120')) {
            return;
        }

        // create a div outside of the visible area and fill it with the selected text.
        var bodyElement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        bodyElement.appendChild(newdiv);
        newdiv.appendChild(selection.getRangeAt(0).cloneContents());

        // we need a <pre> tag workaround.
        // otherwise the text inside "pre" loses all the line breaks!
        if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE' || selection.getRangeAt(0).commonAncestorContainer.nodeName === 'CODE') {
            newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
        }

        var url = document.location.href;
        newdiv.innerHTML += '<br />'
            + '来源: JackWang&#39;s Blog<br />'
            + '文章作者: Jack Wang<br />'
            + '文章链接: <a href="' + url + '">' + url + '</a><br />'
            + '本文章著作权归作者所有，任何形式的转载都请注明出处。';

        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {bodyElement.removeChild(newdiv);}, 200);
    });
</script>


<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('2'),
            headingSelector: 'h1, h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    

    <div class="container row center-align"
         style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2021-2023</span>
            
            <a href="/about" target="_blank">Jack Wang</a>
            <!-- |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a> -->
            <!-- |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a> -->
            <br>
            
                &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                        class="white-color">603.8k</span>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
                <span id="sitetime"> Loading ...</span>
                <script>
                    var calcSiteTime = function () {
                        var seconds = 1000;
                        var minutes = seconds * 60;
                        var hours = minutes * 60;
                        var days = hours * 24;
                        var years = days * 365;
                        var today = new Date();
                        var startYear = "2021";
                        var startMonth = "11";
                        var startDate = "12";
                        var startHour = "0";
                        var startMinute = "0";
                        var startSecond = "0";
                        var todayYear = today.getFullYear();
                        var todayMonth = today.getMonth() + 1;
                        var todayDate = today.getDate();
                        var todayHour = today.getHours();
                        var todayMinute = today.getMinutes();
                        var todaySecond = today.getSeconds();
                        var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                        var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                        var diff = t2 - t1;
                        var diffYears = Math.floor(diff / years);
                        var diffDays = Math.floor((diff / days) - diffYears * 365);

                        // 区分是否有年份.
                        var language = 'zh-CN';
                        if (startYear === String(todayYear)) {
                            document.getElementById("year").innerHTML = todayYear;
                            var daysTip = 'This site has been running for ' + diffDays + ' days';
                            if (language === 'zh-CN') {
                                daysTip = '本站已运行 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                daysTip = '本站已運行 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = daysTip;
                        } else {
                            document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                            var yearsAndDaysTip = 'This site has been running for ' + diffYears + ' years and '
                                + diffDays + ' days';
                            if (language === 'zh-CN') {
                                yearsAndDaysTip = '本站已运行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                yearsAndDaysTip = '本站已運行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = yearsAndDaysTip;
                        }
                    }

                    calcSiteTime();
                </script>
            
            <br>
            
                <span id="icp"><img src="/medias/icp.png"
                                    style="vertical-align: text-bottom;"/>
                <a href="https://beian.miit.gov.cn" target="_blank">陕ICP备2021014294号-1</a>
            </span>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/jackwang0108" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:2232123545@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2232123545" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2232123545" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    

    
        
        <script type="text/javascript">
            // 只在桌面版网页启用特效
            var windowWidth = $(window).width();
            if (windowWidth > 768) {
                document.write('<script type="text/javascript" src="/libs/others/sakura.js"><\/script>');
            }
        </script>
    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
    

     
        <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
        <script src="/libs/others/TencentCaptcha.js"></script>
        <button id="TencentCaptcha" data-appid="xxxxxxxxxx" data-cbfn="callback" type="button" hidden></button>
    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
